<?php

namespace app\common\model;

use think\Model;

/**
 * Order
 */
class Order extends Model
{
    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    
    // 支付状态常量
    const PAY_STATUS = [
        '0' => '未支付',
        '1' => '已支付',
        '2' => '已取消',
    ];
    const PAY_STATUS_UNPAY = '0';
    const PAY_STATUS_PAID = '1';
    const PAY_STATUS_CANCELED = '2';

    const PAYMENT_METHOD_WECHAT = 'wechat';
    const PAYMENT_METHOD_ALIPAY = 'alipay';
    const PAYMENT_METHOD_REDEMPTION = 'redemption'; // 兑换码获取
    
    // 获取方式常量
    const ACQUISITION_TYPE_PAYMENT = 'payment';     // 支付获取
    const ACQUISITION_TYPE_REDEMPTION = 'redemption'; // 兑换码获取
    
    public function scopeStatus($query)
    {
        $query->where('status', 1);
    }
    
    public function getAmountAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\User::class, 'user_id', 'id');
    }
    
    /**
     * 关联兑换码
     * @return \think\model\relation\BelongsTo
     */
    public function redemptionCode(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(RedemptionCode::class, 'redemption_code_id', 'id');
    }
}
